package com.swotplus.admin.web.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.swotplus.framework.web.BaseController;
import com.swotplus.common.annotation.Log;
import com.swotplus.common.core.AjaxResult;
import com.swotplus.common.enums.BusinessType;
import com.swotplus.common.utils.StringUtils;
import com.swotplus.framework.shiro.ShiroUtils;
import com.swotplus.framework.util.poi.ExcelUtil;
import com.swotplus.system.sys.entity.SysDictData;
import com.swotplus.system.sys.service.SysDictDataService;
import com.swotplus.system.sys.vo.SysDictDataVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 数据字典信息
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/system/dict")
public class SysDictDataController extends BaseController
{
    private String prefix = "system/dict/";

    @Autowired
    private SysDictDataService sysDictDataService;

    @RequiresPermissions("system:dict:view")
    @GetMapping()
    public String dictData()
    {
        return prefix + "/list";
    }

    @PostMapping("/list")
    @RequiresPermissions("system:dict:list")
    @ResponseBody
    public AjaxResult list(SysDictDataVo vo)
    {

        Page<SysDictData> page=new Page<>(vo.getPage(),vo.getLimit());
        QueryWrapper<SysDictData> sysDictDataQueryWrapper=new QueryWrapper<>();
        if(StringUtils.isNotBlank(vo.getDictLabel())){
            sysDictDataQueryWrapper.eq("dict_label",vo.getDictLabel());
        }
        if(StringUtils.isNotBlank(vo.getDictType())){
            sysDictDataQueryWrapper.eq("dict_type",vo.getDictType());
        }
        if(StringUtils.isNotBlank(vo.getDictValue())){
            sysDictDataQueryWrapper.eq("dict_value",vo.getDictValue());
        }
        Page<SysDictData> pageList= sysDictDataService.page(page,sysDictDataQueryWrapper);
        return AjaxResult.success("成功", (int) pageList.getTotal(),pageList.getRecords());
    }


    /**
     * 新增字典类型
     */
    @GetMapping("/add")
    public String add( ModelMap mmap)
    {
        return prefix + "/add";
    }

    /**
     * 新增保存字典类型
     */
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @RequiresPermissions("system:dict:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated SysDictData dict)
    {
        dict.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(sysDictDataService.save(dict));
    }

    /**
     * 修改字典类型
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("dict", sysDictDataService.getById(id));
        return prefix + "/edit";
    }

    /**
     * 修改保存字典类型
     */
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:dict:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysDictData dict)
    {
        dict.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(sysDictDataService.updateById(dict));
    }

    @Log(title = "字典数据", businessType = BusinessType.DELETE)
    @RequiresPermissions("system:dict:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysDictDataService.removeById(ids));
    }
}
